package com.foodplus.www.view.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.foodplus.www.adapter.home.HomeAdapter;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.home.DataTypeBean;
import com.foodplus.www.bean.home.HomeBean;
import com.foodplus.www.foodplus.MyApp;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.HomePresenter;
import com.foodplus.www.view.impl.HomeView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements HomeView {


    @BindView(R.id.home_recycler)
    RecyclerView homeRecycler;

    private List<DataTypeBean> data = new ArrayList<>();

    private HomeAdapter adapter;

    private HomePresenter homePresenter;

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initView() {
        homeRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new HomeAdapter(data, getActivity());
        homeRecycler.setAdapter(adapter);
    }

    private void initData() {
        homePresenter = new HomePresenter(this);
        Map<String, String> params = ParamManager.getInstance().getHomeParams();
        homePresenter.getHomeData(params);

    }


    @Override
    public void onSuccess(HomeBean homeBean) {
        data.addAll(homeBean.getDatas().getData_type());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {
        System.out.println();
    }

    @Override
    public void onFinish() {

    }


}
